<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Shopping extends Base{
    //是否开启商城
    public function shopstate(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $aboutus=Db::name('aboutus')->where('cus_id',$cus_id)->find();
        if($aboutus){
            return json(['code' => 200, 'msg' => '成功！','shopstate'=>$aboutus['shopstate']]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //是否存在地址，有则返回地址
    public function isaddress(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        $res=Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where('state','<',3)->select();
        if($res){
            return json(['code' => 200, 'msg' => '存在！','data'=>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '不存在！']);exit();
        }
    }
    //获取默认地址
    public function getaddress(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        $res=Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where('state',1)->find();
        if($res){
            return json(['code' => 200, 'msg' => '存在！','data'=>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '不存在！']);exit();
        }
    }
    //获取地址
    public function getaddressd(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '请给地址id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        $res=Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where('id',$id)->where('state','<',3)->find();
        if($res){
            return json(['code' => 200, 'msg' => '存在！','data'=>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '不存在！']);exit();
        }
    }
    //添加用户地址
    public function addaddress(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.uname"))){
            $uname=input("param.uname");
        }else{
            return json(['code' => 504, 'msg' => '请给姓名']);exit();
        }
        if(!empty(input("param.uphone"))){
            $uphone=input("param.uphone");
        }else{
            return json(['code' => 505, 'msg' => '请给电话']);exit();
        }
        if(!empty(input("param.uaddress"))){
            $uaddress=input("param.uaddress");
        }else{
            return json(['code' => 506, 'msg' => '请给地址']);exit();
        }
        if(!empty(input("param.state"))){
            $state=input("param.state");
        }else{
            return json(['code' => 508, 'msg' => '请给选中状态']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                if($state==1){
                    Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where(['state'=>[1,2]])->update(['state'=>2]);
                    $data['state']=$state;
                }elseif($state==2){
                    $data['state']=$state;
                }
                $data['cus_id']=$cus_id;
                $data['openid']=$openid;
                $data['uname']=$uname;
                $data['uphone']=$uphone;
                $data['uaddress']=$uaddress;
                $data['time']=time();
                $data['uptime']=time();
                $addresss=Db::name('address')->insert($data);
                if($addresss){
                    return json(['code' => 200, 'msg' => '成功保存！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '保存失败！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                if($state==1){
                    Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where(['state'=>[1,2]])->update(['state'=>2]);
                    $data['state']=$state;
                }elseif($state==2){
                    $data['state']=$state;
                }
                $data['cus_id']=$cus_id;
                $data['openid']=$openid;
                $data['uname']=$uname;
                $data['uphone']=$uphone;
                $data['uaddress']=$uaddress;
                $data['time']=time();
                $data['uptime']=time();
                $addresss=Db::name('address')->insert($data);
                if($addresss){
                    return json(['code' => 200, 'msg' => '成功保存！']);exit();
                }else{
                    return json(['code' => 500, 'msg' => '保存失败！']);exit();
                }
            }else{
                return json(['code' => 507, 'msg' => '未开启此平台！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //修改用户地址
    public function editaddress(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 503, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.uname"))){
            $uname=input("param.uname");
        }else{
            return json(['code' => 504, 'msg' => '请给姓名']);exit();
        }
        if(!empty(input("param.uphone"))){
            $uphone=input("param.uphone");
        }else{
            return json(['code' => 505, 'msg' => '请给电话']);exit();
        }
        if(!empty(input("param.uaddress"))){
            $uaddress=input("param.uaddress");
        }else{
            return json(['code' => 506, 'msg' => '请给地址']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 508, 'msg' => '请给id']);exit();
        }
        if(!empty(input("param.state"))){
            $state=input("param.state");
        }else{
            return json(['code' => 509, 'msg' => '请给选中状态']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $isaddress=Db::name('address')->where('id',$id)->where('cus_id',$cus_id)->where('openid',$openid)->find();
                if($isaddress){
                    if($state==1){
                        Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where(['state'=>[1,2]])->update(['state'=>2]);
                        $data['state']=$state;
                    }elseif($state==2){
                        $data['state']=$state;
                    }
                    $data['uname']=$uname;
                    $data['uphone']=$uphone;
                    $data['uaddress']=$uaddress;
                    $data['uptime']=time();
                    $addresss=Db::name('address')->where('id',$id)->update($data);
                    if($addresss){
                        return json(['code' => 200, 'msg' => '修改成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '修改失败！']);exit();
                    }
                }else{
                    return json(['code' => 509, 'msg' => '不存在此地址']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $isaddress=Db::name('address')->where('id',$id)->where('cus_id',$cus_id)->where('openid',$openid)->find();
                if($isaddress){
                    if($state==1){
                        Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->where(['state'=>[1,2]])->update(['state'=>2]);
                        $data['state']=$state;
                    }elseif($state==2){
                        $data['state']=$state;
                    }
                    $data['uname']=$uname;
                    $data['uphone']=$uphone;
                    $data['uaddress']=$uaddress;
                    $data['uptime']=time();
                    $addresss=Db::name('address')->where('id',$id)->update($data);
                    if($addresss){
                        return json(['code' => 200, 'msg' => '修改成功！']);exit();
                    }else{
                        return json(['code' => 500, 'msg' => '修改失败！']);exit();
                    }
                }else{
                    return json(['code' => 509, 'msg' => '不存在此地址']);exit();
                }
            }else{
                return json(['code' => 507, 'msg' => '未开启此平台！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //选中默认地址
    public function seladdress(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 502, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给需要默认的地址id']);exit();
        }
        $res=Db::name('address')->where('cus_id',$cus_id)->where('openid',$openid)->update(['state'=>2]);
        if($res){
            $resid=Db::name('address')->where('id',$id)->where('cus_id',$cus_id)->where('openid',$openid)->update(['state'=>1]);
            if($resid){
                return json(['code' => 200, 'msg' => '成功！']);exit();
            }else{
                return json(['code' => 500, 'msg' => '失败！']);exit();
            }
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //删除地址
    public function deladdress(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 502, 'msg' => '请给openid']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '请给需要删除的地址id']);exit();
        }
        $res=Db::name('address')->where('id',$id)->where('cus_id',$cus_id)->where('openid',$openid)->update(['state'=>3]);
        if($res){
            return json(['code' => 200, 'msg' => '成功！']);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
    //下单
    // public function orderin(){
    //     if(!empty(input("param.cus_id"))){
    //         $cus_id=input("param.cus_id");
    //     }else{
    //         return json(['code' => 501, 'msg' => '请给出客户id']);exit();
    //     }
    //     if(!empty(input("param.openid"))){
    //         $openid=input("param.openid");
    //     }else{
    //         return json(['code' => 502, 'msg' => '请给openid']);exit();
    //     }
    //     if(!empty(input("param.p_id"))){
    //         $p_id=input("param.p_id");
    //     }else{
    //         return json(['code' => 503, 'msg' => '请给产品id']);exit();
    //     }
    //     if(!empty(input("param.formid"))){
    //         $formid=input("param.formid");
    //     }else{
    //         return json(['code' => 504, 'msg' => '请给formid']);exit();
    //     }
    //     if(!empty(input("param.address_id"))){
    //         $address_id=input("param.address_id");
    //     }else{
    //         return json(['code' => 505, 'msg' => '请给地址id']);exit();
    //     }
    //     if(!empty(input("param.price"))){
    //         $price=input("param.price");
    //     }else{
    //         return json(['code' => 506, 'msg' => '请给单价']);exit();
    //     }
    //     if(!empty(input("param.buynum"))){
    //         $buynum=input("param.buynum");
    //     }else{
    //         return json(['code' => 507, 'msg' => '请给购买数量']);exit();
    //     }
    //     if(!empty(input("param.payfee"))){
    //         $payfee=input("param.payfee");
    //     }else{
    //         return json(['code' => 508, 'msg' => '请给支付总价']);exit();
    //     }
    //     if(!empty(input("param.parm"))){
    //         $parm=input("param.parm");
    //     }else{
    //         return json(['code' => 509, 'msg' => '请给产品参数']);exit();
    //     }
    //     $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
    //     if($customer){
    //         if(input("param.platform")==1&&$customer['platformwx_state']==1){
    //             $checkdata = [
    //                 'cus_id' =>$cus_id,
    //                 'openid' =>$openid,
    //                 'p_id' =>$p_id,
    //                 'formid' =>$formid,
    //                 'address_id' =>$address_id,
    //                 'price' => $price,
    //                 'buynum' => $buynum,
    //                 'payfee' => $payfee,
    //                 'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
    //                 'parm' => $parm,
    //                 'paystate' =>1,
    //                 'time' => time(),
    //                 'uptime' => time(),
    //             ];
    //             $insertid = Db::name('orders')->insertGetId($checkdata);
    //             if($insertid){
    //                 return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$insertid, 'paycode' =>$checkdata['paycode']]);exit();
    //             }else{
    //                 return json(['code' => 500, 'msg' => '失败！']);exit();
    //             }
    //         }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
    //             $checkdata = [
    //                 'cus_id' =>$cus_id,
    //                 'openid' =>$openid,
    //                 'p_id' =>$p_id,
    //                 'formid' =>$formid,
    //                 'address_id' =>$address_id,
    //                 'price' => $price,
    //                 'buynum' => $buynum,
    //                 'payfee' => $payfee,
    //                 'paycode' => 'T'.date('YmdHis',time()).mt_rand(000,999),
    //                 'parm' => $parm,
    //                 'paystate' =>1,
    //                 'time' => time(),
    //                 'uptime' => time(),
    //             ];
    //             $insertid = Db::name('orders')->insertGetId($checkdata);
    //             if($insertid){
    //                 return json(['code' => 200, 'msg' => '成功！', 'order_id' =>$insertid, 'paycode' =>$checkdata['paycode']]);exit();
    //             }else{
    //                 return json(['code' => 500, 'msg' => '失败！']);exit();
    //             }
    //         }else{
    //             return json(['code' => 511, 'msg' => '未开启此平台！']);exit();
    //         }
    //     }else{
    //         return json(['code' => 510, 'msg' => '此客户不存在或者被禁用了！']);exit();
    //     }
    // }
    public function shopprosearch(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.key"))){
            $key=input('param.key');
        }else{
            return json(['code' => 503, 'msg' => '搜索关键词呢']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        if(!empty(input("param.type"))){
            $type=input("param.type");
        }else{
            return json(['code' => 505, 'msg' => '综合？价格？最新？还是销量？']);exit();
        }
        if($type==2){
            if(!empty(input("param.price"))){
                $price=input("param.price");
            }else{
                $price=0;
            }
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                if($type==1){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price asc')->order('id desc')->order('sales desc')->limit(4*($page-1),4)->select();
                }
                if($type==2){
                    if($price==0){
                        $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price asc')->limit(4*($page-1),4)->select();
                    }else{
                        $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price desc')->limit(4*($page-1),4)->select();
                    }
                }
                if($type==3){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                }
                if($type==4){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('sales desc')->limit(4*($page-1),4)->select();
                }
                if($products){
                    for ($i=0; $i <count($products) ; $i++) {
                        $products[$i]['time']=date('Y/m/d',$products[$i]['time']);
                        $products[$i]['uptime']=date('Y-m-d H:i:s',$products[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                if($type==1){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price asc')->order('id desc')->order('sales desc')->limit(4*($page-1),4)->select();
                }
                if($type==2){
                    if($price==0){
                        $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price asc')->limit(4*($page-1),4)->select();
                    }else{
                        $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price desc')->limit(4*($page-1),4)->select();
                    }
                }
                if($type==3){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                }
                if($type==4){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('sales desc')->limit(4*($page-1),4)->select();
                }
                if($products){
                    for ($i=0; $i <count($products) ; $i++) {
                        $products[$i]['time']=date('Y/m/d',$products[$i]['time']);
                        $products[$i]['uptime']=date('Y-m-d H:i:s',$products[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                if($type==1){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price asc')->order('id desc')->order('sales desc')->limit(4*($page-1),4)->select();
                }
                if($type==2){
                    if($price==0){
                        $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price asc')->limit(4*($page-1),4)->select();
                    }else{
                        $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('price desc')->limit(4*($page-1),4)->select();
                    }
                }
                if($type==3){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                }
                if($type==4){
                    $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('sales desc')->limit(4*($page-1),4)->select();
                }
                if($products){
                    for ($i=0; $i <count($products) ; $i++) {
                        $products[$i]['time']=date('Y/m/d',$products[$i]['time']);
                        $products[$i]['uptime']=date('Y-m-d H:i:s',$products[$i]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //收费需要邮费
    public function expressfee(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $data['expressfeestate']=$customer['expressfeestate'];
            $data['expressfee']=$customer['expressfee'];
            return json(['code' => 200, 'msg' => '查询成功！','data'=>$data]);exit();
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //产品详情-评论
    public function productdetaileval(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出产品id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $evaluates=Db::name('evaluate')->alias('a')->join('users b', 'a.openid = b.openid','LEFT')->field('a.*,b.avatar,b.nickname')->where('a.p_id',$p_id)->order('a.id desc')->limit(4*($page-1),4)->select();
                if($evaluates){
                    for ($i=0; $i <count($evaluates); $i++) {
                        $evaluates[$i]['time']=date('Y-m-d',$evaluates[$i]['time']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$evaluates]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $evaluates=Db::name('evaluate')->alias('a')->join('users b', 'a.openid = b.openid','LEFT')->field('a.*,b.avatar,b.nickname')->where('a.p_id',$p_id)->order('a.id desc')->limit(4*($page-1),4)->select();
                if($evaluates){
                    for ($i=0; $i <count($evaluates); $i++) {
                        $evaluates[$i]['time']=date('Y-m-d',$evaluates[$i]['time']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$evaluates]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $evaluates=Db::name('evaluate')->alias('a')->join('users b', 'a.openid = b.openid','LEFT')->field('a.*,b.avatar,b.nickname')->where('a.p_id',$p_id)->order('a.id desc')->limit(4*($page-1),4)->select();
                if($evaluates){
                    for ($i=0; $i <count($evaluates); $i++) {
                        $evaluates[$i]['time']=date('Y-m-d',$evaluates[$i]['time']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$evaluates]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    public function cates(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $data=Db::name('shopcate')->where('cus_id',$cus_id)->where('f_id',0)->where('state',1)->select();
            if($data){
                return json(['code' => 200, 'msg' => '成功！','data'=>$data]);exit();
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    public function subcates(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.f_id"))){
            $f_id=input("param.f_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出父级id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            $data=Db::name('shopcate')->where('cus_id',$cus_id)->where('f_id',$f_id)->where('state',1)->select();
            if($data){
                return json(['code' => 200, 'msg' => '成功！','data'=>$data]);exit();
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //商品推荐
    public function goodsflag(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 503, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('sort desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('sort desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('sort desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //商品所有对应的商品
    public function goodsall(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 503, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //新品
    public function goodsnew(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 503, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('time','>',time()-7*86400)->order('id desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('time','>',time()-7*86400)->order('id desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('shopgoods')->where('cus_id',$cus_id)->where('state',1)->where('time','>',time()-7*86400)->order('id desc')->limit(6*($page-1),6)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //商品分类名对应的商品
    public function categoods(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.cate_id"))){
            $cate_id=input("param.cate_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出分类名的id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('shopgoods')->where('cate_id',$cate_id)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('shopgoods')->where('cate_id',$cate_id)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('shopgoods')->where('cate_id',$cate_id)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //商品跳转详情
    public function goodsdetail(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 503, 'msg' => '商品详情id呢']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $detail=Db::name('shopgoods')->where('id',$id)->where('cus_id',$cus_id)->where('state',1)->find();
                if($detail){
                    $detail['uptime']=date('Y-m-d H:i:s',$detail['uptime']);
                    $detail['content'] = str_replace('<img src="','<img src="'.config('tmdhttp').$_SERVER['HTTP_HOST'],$detail['content']);
                    $detail['content'] = str_replace('<source src="','<source src="'.config('tmdhttp').$_SERVER['HTTP_HOST'],$detail['content']);
                    $detail['attrarrs']=json_decode($detail['attrarrs']);
                    if ($detail['attrarrs']) {
                        for ($i=0; $i <count($detail['attrarrs']) ; $i++) { 
                            $detail['attrarrs'][$i]->attrvals=explode(",",$detail['attrarrs'][$i]->attrval);
                            for ($j=0; $j <count($detail['attrarrs'][$i]->attrvals); $j++) { 
                                $detail['attrarrs'][$i]->attrvalsel[$j]=0;
                            }
                        }
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$detail]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $detail=Db::name('shopgoods')->where('id',$id)->where('cus_id',$cus_id)->where('state',1)->find();
                if($detail){
                    $detail['content'] = str_replace('<img src="','<img src="'.config('tmdhttp').$_SERVER['HTTP_HOST'],$detail['content']);
                    $detail['content'] = str_replace('<source src="','<source src="'.config('tmdhttp').$_SERVER['HTTP_HOST'],$detail['content']);
                    $detail['uptime']=date('Y-m-d H:i:s',$detail['uptime']);
                    $detail['attrarrs']=json_decode($detail['attrarrs']);
                    if ($detail['attrarrs']) {
                        for ($i=0; $i <count($detail['attrarrs']) ; $i++) { 
                            $detail['attrarrs'][$i]->attrvals=explode(",",$detail['attrarrs'][$i]->attrval);
                            for ($j=0; $j <count($detail['attrarrs'][$i]->attrvals); $j++) { 
                                $detail['attrarrs'][$i]->attrvalsel[$j]=0;
                            }
                        }
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$detail]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $detail=Db::name('shopgoods')->where('id',$id)->where('cus_id',$cus_id)->where('state',1)->find();
                if($detail){
                    $detail['content'] = str_replace('<img src="','<img src="'.config('tmdhttp').$_SERVER['HTTP_HOST'],$detail['content']);
                    $detail['content'] = str_replace('<source src="','<source src="'.config('tmdhttp').$_SERVER['HTTP_HOST'],$detail['content']);
                    $detail['uptime']=date('Y-m-d H:i:s',$detail['uptime']);
                    $detail['attrarrs']=json_decode($detail['attrarrs']);
                    if ($detail['attrarrs']) {
                        for ($i=0; $i <count($detail['attrarrs']) ; $i++) { 
                            $detail['attrarrs'][$i]->attrvals=explode(",",$detail['attrarrs'][$i]->attrval);
                            for ($j=0; $j <count($detail['attrarrs'][$i]->attrvals); $j++) { 
                                $detail['attrarrs'][$i]->attrvalsel[$j]=0;
                            }
                        }
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$detail]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
}